package com.easipass.lgsa.portRelease.util;

import com.alibaba.fastjson.JSON;
import com.easipass.bscp.dto.*;
import com.easipass.lgsa.portRelease.feign.OrgService;
import com.easipass.lgsa.portRelease.model.PortReleaseBscModel;
import com.easipass.lgsa.portRelease.model.PortReleaseDtModel;
import com.easipass.lgsa.portRelease.model.PortReleaseInexpRltModel;
import com.easipass.lgsa.portRelease.model.PortReleaseRltModel;
import com.easipass.lgsa.portRelease.service.PortReleaseDtService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.swagger.models.Model;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.ArrayList;
import java.util.List;

@Component
public class TransUtil {

    private final Log log = LogFactory.getLog(this.getClass());
    private Gson gson = new GsonBuilder().serializeNulls().create();
    @Autowired
    private OrgService orgService;

    @Autowired
    private PortReleaseDtService portReleaseDtService;

    public InventoryDto portBscToInvDto(PortReleaseBscModel bscModel) {
        InventoryDto inventoryDto = new InventoryDto();
        inventoryDto.setOrgId(bscModel.getOrgId().toString());
        inventoryDto.setBusinessNo(bscModel.getPreentNo());
        inventoryDto.setBusinessType("portRelease");
        inventoryDto.setInExpType(bscModel.getInExpType());
        inventoryDto.setIeTypecd(bscModel.getIeTypecd());
        List<PortReleaseDtModel> dtByid = portReleaseDtService.getDtByid(bscModel.getId());
        List<BusinessDtListDtos> businessDtListDtosList = new ArrayList<>();
        dtByid.forEach(dt -> {
            BusinessDtListDtos businessDtListDtos = new BusinessDtListDtos();
            businessDtListDtos.setDclQty(dt.getDclQty().toString());
            businessDtListDtos.setGdsMtno(dt.getGdsMtno());
            businessDtListDtos.setDclUnitcd(dt.getDclUnitcd());
            businessDtListDtosList.add(businessDtListDtos);
        });
        inventoryDto.getBusinessDtListDtos().addAll(businessDtListDtosList);
        return inventoryDto;
    }

    public Map<String, String> portBscToMap(PortReleaseBscModel bscModel) {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("orgId", bscModel.getOrgId().toString());
        hashMap.put("businessNo", bscModel.getPreentNo());
        hashMap.put("inExpType", bscModel.getInExpType());
        hashMap.put("ieTypecd", bscModel.getIeTypecd());
        hashMap.put("optType", "2");
        List<PortReleaseDtModel> dtByid = portReleaseDtService.getDtByid(bscModel.getId());
        List<BusinessDtListDtos> businessDtListDtosList = new ArrayList<>();
        dtByid.forEach(dt -> {
            BusinessDtListDtos businessDtListDtos = new BusinessDtListDtos();
            businessDtListDtos.setDclQty(dt.getDclQty().toString());
            businessDtListDtos.setGdsMtno(dt.getGdsMtno());
            businessDtListDtos.setDclUnitcd(dt.getDclUnitcd());
            businessDtListDtosList.add(businessDtListDtos);
        });
        hashMap.put("businessDtListDtos", businessDtListDtosList.toString());
        return hashMap;
    }

    public List<PortReleaseDtModel> dtListToDtModelList(List<PortReleaseDt> dtList) throws Exception {
        List<PortReleaseDtModel> dtModelList = new ArrayList<>();
        if (null != dtList && dtList.size() > 0) {
            dtList.forEach(dt -> {
                dtModelList.add(this.transObj(dt, PortReleaseDtModel.class));
            });
        }
        return dtModelList;
    }

    public List<PortReleaseInexpRltModel> inexpRltListToModelList(List<PortReleaseInexpRlt> inexpRltList) throws Exception {
        List<PortReleaseInexpRltModel> inexpRltModelList = new ArrayList<>();
        if (null != inexpRltList && inexpRltList.size() > 0) {
            inexpRltList.forEach(inexpRlt -> {
                inexpRltModelList.add(this.transObj(inexpRlt, PortReleaseInexpRltModel.class));
            });
        }
        return inexpRltModelList;
    }

    public List<PortReleaseInexpRlt> inexpRltModelListToInexpRltList(List<PortReleaseInexpRltModel> inexpRltModels) throws Exception {
        List<PortReleaseInexpRlt> inexpRltList = new ArrayList<>();
        if (null != inexpRltModels && inexpRltModels.size() > 0) {
            inexpRltModels.forEach(inexpRltModel -> {
                inexpRltList.add(this.transObj(inexpRltModel, PortReleaseInexpRlt.class));
            });
        }
        return inexpRltList;
    }

    public List<com.easipass.bscp.msg.PortReleaseInexpRlt> msgInexpRltModelListToInexpRltList(List<PortReleaseInexpRltModel> inexpRltModels) throws Exception {
        List<com.easipass.bscp.msg.PortReleaseInexpRlt> inexpRltList = new ArrayList<>();
        if (null != inexpRltModels && inexpRltModels.size() > 0) {
            inexpRltModels.forEach(inexpRltModel -> {
                inexpRltList.add(this.transObj(inexpRltModel, com.easipass.bscp.msg.PortReleaseInexpRlt.class));
            });
        }
        return inexpRltList;
    }

    public List<PortReleaseDt> dtModelListToDtList(List<PortReleaseDtModel> dtModelList) throws Exception {
        List<PortReleaseDt> dtList = new ArrayList<>();
        if (null != dtModelList && dtModelList.size() > 0) {
            dtModelList.forEach(dtModel -> {
                dtList.add(this.transObj(dtModel, PortReleaseDt.class));
            });
        }
        return dtList;
    }

    public List<PortReleaseRltModel> rltListToRltModelList(List<PortReleaseRlt> rltList) throws Exception {
        List<PortReleaseRltModel> rltModelList = new ArrayList<>();
        if (null != rltList && rltList.size() > 0) {
            rltList.forEach(rlt -> {
                rltModelList.add(this.transObj(rlt, PortReleaseRltModel.class));
            });
        }
        return rltModelList;
    }

    public List<PortReleaseRlt> rltModelListToRltList(List<PortReleaseRltModel> rltModelList) throws Exception {
        List<PortReleaseRlt> rltList = new ArrayList<>();
        if (null != rltModelList && rltModelList.size() > 0) {
            rltModelList.forEach(rltModel -> {
                rltList.add(this.transObj(rltModel, PortReleaseRlt.class));
            });
        }
        return rltList;
    }

    public PortReleaseBsc bscModelToDto(PortReleaseBscModel bscModel) throws Exception {
        return this.transObj(bscModel, PortReleaseBsc.class);
    }

    public PortReleaseBscModel bscDtoToModel(PortReleaseBsc bsc) throws Exception {
        return this.transObj(bsc, PortReleaseBscModel.class);
    }

    public PortReleaseBscModel bscMsgToModel(com.easipass.bscp.msg.PortReleaseBsc portReleaseBsc) throws Exception {
        return this.transObj(portReleaseBsc, PortReleaseBscModel.class);
    }

    public com.easipass.bscp.msg.PortReleaseBsc bscMdoelTMsg(PortReleaseBscModel bscModel) throws Exception {
        return this.transObj(bscModel, com.easipass.bscp.msg.PortReleaseBsc.class);
    }

    public List<com.easipass.bscp.msg.PortReleaseDt> dtModelListToDtMsgList(List<PortReleaseDtModel> dtModelList) {
        List<com.easipass.bscp.msg.PortReleaseDt> dtMsgList = new ArrayList<>();
        for (PortReleaseDtModel dtModel : dtModelList) {
            dtMsgList.add(this.transObj(dtModel, com.easipass.bscp.msg.PortReleaseDt.class));
        }
        return dtMsgList;
    }

    public List<com.easipass.bscp.msg.PortReleaseRlt> rltModelListToRltMsgList(List<PortReleaseRltModel> rltModelList) {
        List<com.easipass.bscp.msg.PortReleaseRlt> rltMsgList = new ArrayList<>();
        for (PortReleaseRltModel rltModel : rltModelList) {
            rltMsgList.add(this.transObj(rltModel, com.easipass.bscp.msg.PortReleaseRlt.class));
        }
        return rltMsgList;
    }

    public <T> T transObj(Object obj, Class<T> classOfT) {
        return JSON.parseObject(JSON.toJSONString(obj), classOfT);
    }


    // 去除空格字符串，设置为null
    public Object removeSpace(Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();
        if (fields.length > 0) {
            for (Field field : fields) {
                if (String.class == field.getType()) {
                    field.setAccessible(true);
                    String name = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
                    Method getDestMethod = null;
                    try {
                        getDestMethod = obj.getClass().getMethod("get" + name);
                        Object objValue = getDestMethod.invoke(obj);
                        if (null != objValue && StringUtils.isBlank(objValue.toString())) {
                            field.set(obj, null);
                        }
                    } catch (NoSuchMethodException ignored) {
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }
            }
        }
        return obj;
    }

}
